package com.ssm.juc.lock;

import com.ssm.juc.java8.locks.Lock;
import com.ssm.juc.java8.locks.ReentrantLock;


/**
 * ReentrantLock 加锁
 */
public class ReentrantLockTest {
    private static int sum = 0;
    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 4; i++) {
            Thread thread = new Thread(() -> {
                //加锁
                lock.lock();
                try {
                    for (int j = 0; j < 100000000; j++) {
                        sum++;
                    }
                } finally {
                    //解锁
                    lock.unlock();
                }
            });
            thread.start();
        }
        Thread.sleep(2000);
        System.out.println(sum);
    }
}